package com.fwlst.module_home.screen

import android.app.Activity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.alibaba.android.arouter.launcher.ARouter
import com.fwlst.lib_base.constant.AppConfig
import com.fwlst.lib_base.ui.NativeTitleBar
import com.fwlst.module_home.R
import com.fwlst.module_home.model.MemberViewModel
import com.fwlst.module_home.ui.GoodItem
import com.hjq.toast.Toaster
import java.math.BigDecimal
import java.math.RoundingMode

@Composable
fun MemberCenterScreen(viewModel: MemberViewModel) {
    val context = LocalContext.current
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color.White)
    ) {
        Image(
            painter = painterResource(id = R.drawable.home_member_center_bg),
            contentDescription = null,
            modifier = Modifier.fillMaxWidth(),
            contentScale = ContentScale.FillWidth
        )
        Image(
            painter = painterResource(id = R.drawable.home_member_center_desc),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 59.dp)
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = 29.dp, bottom = 120.dp)
        ) {
            NativeTitleBar(title = "会员中心",
                backgroundColor = android.graphics.Color.TRANSPARENT,
                onBackClick = { (context as Activity).finish() })
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(top = 180.dp, start = 25.dp, end = 25.dp)
            ) {
                item {
                    Image(painter = painterResource(id = R.drawable.home_member_benefits),
                        contentDescription = null,
                        modifier = Modifier
                            .fillMaxWidth()
                            .clickable {
                                Toaster.show("此处只是权益介绍，相关功能请进入功能页面使用")
                            })
                    Text(
                        text = "会员充值",
                        fontSize = 20.sp,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 20.dp)
                    )
                    if (viewModel.goodsList.isNotEmpty()){
                        LazyRow(
                            modifier = Modifier
                                .fillMaxWidth()
                                .background(color = Color.White)
                        ) {
                            items(viewModel.goodsList) {
                                GoodItem(viewModel, it)
                            }
                        }
                    }

                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 20.dp)
                    ) {
                        Text(
                            text = "支付方式", fontSize = 20.sp, modifier = Modifier.weight(1f)
                        )
                        Text(text = "购买须知", fontSize = 14.sp, modifier = Modifier.clickable {
                            ARouter.getInstance().build("/base/route/WebViewActivity")
                                .withString("url", AppConfig.USER_PROTOCOL_URL).navigation()
                        })
                    }

                    Column {
                        viewModel.payTypes.forEach {
                            Row(
                                modifier = Modifier
                                    .padding(bottom = 15.dp)
                                    .clickable {
                                        viewModel.selectPayType(it)
                                    }, verticalAlignment = Alignment.CenterVertically
                            ) {
                                Image(
                                    painter = painterResource(id = it.icon),
                                    contentDescription = null,
                                    modifier = Modifier.size(25.dp)
                                )
                                Text(
                                    modifier = Modifier
                                        .weight(1f)
                                        .padding(start = 10.dp),
                                    text = it.name,
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight.Bold
                                )
                                Image(
                                    painter = painterResource(id = if (it.selected) com.fwlst.lib_base.R.drawable.base_member_selected else com.fwlst.lib_base.R.drawable.base_member_no_selected),
                                    contentDescription = null,
                                    modifier = Modifier.size(20.dp)
                                )
                            }
                        }
                    }
                }
            }

        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.BottomCenter)
                .background(color = Color.White)
                .border(1.dp, Color.Gray)
        ) {
            if (AppConfig.APP_MARKETS == 1) {
                Row(modifier = Modifier
                    .clickable {
                        viewModel.confirmSelected = !viewModel.confirmSelected
                    }
                    .fillMaxWidth()
                    .padding(start = 28.dp, end = 28.dp, top = 15.dp),
                    verticalAlignment = Alignment.CenterVertically) {
                    Image(
                        painter = painterResource(id = if (viewModel.confirmSelected) com.fwlst.lib_base.R.drawable.base_member_selected else com.fwlst.lib_base.R.drawable.base_member_no_selected),
                        contentDescription = null,
                        modifier = Modifier.size(20.dp)
                    )
                    Text(
                        modifier = Modifier
                            .padding(start = 10.dp)
                            .weight(1f),
                        text = "已确定并同意开通所选会员特权服务",
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Bold,
                    )
                }
            }
            Button(
                onClick = { viewModel.preOrder(context) },
                colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF88B6FF)),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(
                        start = 28.dp,
                        end = 28.dp,
                        bottom = 20.dp,
                        top = if (AppConfig.APP_MARKETS == 1) 15.dp else 20.dp
                    )
            ) {
                if (viewModel.selectedGoods != null){
                    Row {
                        Text(
                            text = "¥${viewModel.selectedGoods!!.goods_price}立即开通",
                            fontSize = 24.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Box(
                            modifier = Modifier.height(32.dp)
                        ) {
                            val originalPrice =
                                BigDecimal.valueOf(viewModel.selectedGoods!!.original_price.toDouble())
                            val goodsPrice =
                                BigDecimal.valueOf(viewModel.selectedGoods!!.goods_price.toDouble())
                            val discount = originalPrice.subtract(goodsPrice)
                                .setScale(2, RoundingMode.HALF_UP)
                            Text(
                                text = "（已省¥${discount}）",
                                fontSize = 14.sp,
                                modifier = Modifier.align(Alignment.BottomStart)
                            )
                        }
                    }
                }

            }
        }

        if (viewModel.showPayConfirm) {
            AlertDialog(onDismissRequest = {}, title = {
                Text(
                    modifier = Modifier.fillMaxWidth(),
                    text = "确认开通？",
                    textAlign = TextAlign.Center
                )
            }, text = {
                Text(text = "我已确定并同意开通所选会员特权服务")
            }, confirmButton = {
                Button(
                    onClick = {
                        viewModel.showPayConfirm = false
                        viewModel.confirmSelected = true
                        viewModel.preOrder(context)
                    }, colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF88B6FF))
                ) {
                    Text(text = "确认开通")
                }
            })
        }

        if (viewModel.isLoading){
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Black.copy(alpha = 0.5f)),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator(color = Color.White)
            }
        }
    }

}